package com.jwthhealth_pub.old.report.view.adapter;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.old.report.model.resbody.SportSuggVideoResp;
import com.jwthhealth_pub.old.report.view.SportDetailExplActivity;

import java.util.List;

/**
 * 运动说明
 * Created by liuzhen on 2017/10/25.
 */

public class SportExplainAdapter extends RecyclerView.Adapter {
    private final SportDetailExplActivity mActivity;
    private final List<SportSuggVideoResp.DataBean.ListBean.MainDescBean> mData;

    public SportExplainAdapter(SportDetailExplActivity sportDetailExplActivity, List<SportSuggVideoResp.DataBean.ListBean.MainDescBean> main_desc) {
        this.mActivity = sportDetailExplActivity;
        this.mData = main_desc;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

//        if (viewType == TYPE.title.ordinal())
//            return new TitleViewHolder(ViewHolder.inflate(mActivity, R.layout.item_sport_explain_title, null));
//        else
        return new ContentViewHolder(View.inflate(mActivity, R.layout.item_sport_explain_list_content, null));

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        SportSuggVideoResp.DataBean.ListBean.MainDescBean mainDescBean = mData.get(position);
        if (mainDescBean == null) return;

//        if (holder instanceof TitleViewHolder) {
//
//            TitleViewHolder vh = (TitleViewHolder) holder;
//            if (mainDescBean.getName() != null){
//                vh.title.setText(mainDescBean.getName());
//                Log.d("SportExplainAdapter", mainDescBean.getName());
//            }
//        } else {
        ContentViewHolder vh = (ContentViewHolder) holder;
        if (mainDescBean.getName() != null)
            vh.title.setText(mainDescBean.getName());
        if (mainDescBean.getDesc() != null && mainDescBean.getDesc().size() >= 0) {
            vh.rvList.setLayoutManager(new LinearLayoutManager(mActivity));
            vh.rvList.setAdapter(new InnerAdapter(mainDescBean.getDesc()));
//            }

        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

//    @Override
//    public int getItemViewType(int position) {
//
//        if (position % 2 == 0)
//            return TYPE.title.ordinal();
//        else
//            return TYPE.content.ordinal();
//
//    }


    private class TitleViewHolder extends RecyclerView.ViewHolder {

        TextView title;

        TitleViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
        }
    }

    private class ContentViewHolder extends RecyclerView.ViewHolder {

        TextView title;
        RecyclerView rvList;

        ContentViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.tv_title);
            rvList = itemView.findViewById(R.id.rv_list);
        }
    }

//    private enum TYPE {
//        title, content;
//    }

    private class InnerAdapter extends RecyclerView.Adapter {


        private final List<String> desc;

        InnerAdapter(List<String> desc) {
            this.desc = desc;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new InnerViewHolder(View.inflate(mActivity, R.layout.item_sport_explain_spec_content, null));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            InnerViewHolder vh = (InnerViewHolder) holder;
            String s = desc.get(position);
            if (s != null)
                vh.content.setText(s);
        }

        @Override
        public int getItemCount() {
            return desc.size();
        }

        private class InnerViewHolder extends RecyclerView.ViewHolder {

            private TextView content;

            public InnerViewHolder(View itemView) {
                super(itemView);
                content = itemView.findViewById(R.id.content);
            }
        }
    }
}
